﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Entities;
using BLL;

namespace Presentation
{
    public partial class DetailViewHistory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Load();
            }
                
        }

        protected void gvView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            int newPage;
            newPage = e.NewPageIndex;
            if (newPage < 0 || newPage >= gvView.PageCount)
            {
                e.Cancel = true;
            }
            else
            {
                gvView.PageIndex = newPage;
                gvView.DataBind();
            }
            Load();
        }

        protected void gvView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

        }
        protected void Load()
        {
            LogBLL lB = new LogBLL();
                CardsEntity cE = new CardsEntity();
                cE.CardNo = Session["CardNo"].ToString();
                List<LogsEntity> listLog = lB.ReadLog(cE);
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("ATM(Location)", typeof(string)));
                dt.Columns.Add(new DataColumn("Type", typeof(string)));
                dt.Columns.Add(new DataColumn("Date", typeof(string)));
                dt.Columns.Add(new DataColumn("Amount", typeof(string)));
                dt.Columns.Add(new DataColumn("Details", typeof(string)));
                DateTime ngayHT = DateTime.Now;               
                if (this.Request.Params["id"] == "1")
                {                   
                    for (int i = 0; i < listLog.Count; i++)
                    {
                        DateTime ngayGD = listLog[i].LogDate;
                        TimeSpan time = ngayHT - ngayGD;                       
                        if (time.Days <= 7)
                        {
                            DataRow row = dt.NewRow();
                            row["ATM(Location)"] = listLog[i].ATM.ATMAddress;
                            row["Type"] = listLog[i].LogType.Description;
                            row["Date"] = listLog[i].LogDate;
                            row["Amount"] = listLog[i].Amount;
                            row["Details"] = listLog[i].Details;
                            dt.Rows.Add(row);
                        }
                    }
                    gvView.DataSource = dt;
                    gvView.DataBind();
                }

                if (this.Request.Params["id"] == "2")
                {
                    for (int i = 0; i < listLog.Count; i++)
                    {
                        DateTime ngayGD = listLog[i].LogDate;
                        TimeSpan time = ngayHT - ngayGD;
                        if (time.Days <= 30)
                        {
                            DataRow row = dt.NewRow();
                            row["ATM(Location)"] = listLog[i].ATM.ATMAddress;
                            row["Type"] = listLog[i].LogType.Description;
                            row["Date"] = listLog[i].LogDate;
                            row["Amount"] = listLog[i].Amount;
                            row["Details"] = listLog[i].Details;
                            dt.Rows.Add(row);
                        }
                    }
                    gvView.DataSource = dt;
                    gvView.DataBind();
                }

                if (this.Request.Params["id"] == "3")
                {
                    for (int i = 0; i < listLog.Count; i++)
                    {
                        DateTime ngayGD = listLog[i].LogDate;
                        TimeSpan time = ngayHT - ngayGD;
                        if (time.Days <= 60)
                        {
                            DataRow row = dt.NewRow();
                            row["ATM(Location)"] = listLog[i].ATM.ATMAddress;
                            row["Type"] = listLog[i].LogType.Description;
                            row["Date"] = listLog[i].LogDate;
                            row["Amount"] = listLog[i].Amount;
                            row["Details"] = listLog[i].Details;
                            dt.Rows.Add(row);
                        }
                    }
                    gvView.DataSource = dt;
                    gvView.DataBind();
                }

                if (this.Request.Params["id"] == "4")
                {                  
                    for (int i = 0; i < listLog.Count; i++)
                    {                       
                        DateTime ngayGD = listLog[i].LogDate;
                        TimeSpan time = ngayHT - ngayGD;
                        if (time.Days <= 180)
                        {
                            DataRow row = dt.NewRow();
                            row["ATM(Location)"] = listLog[i].ATM.ATMAddress;
                            row["Type"] = listLog[i].LogType.Description;
                            row["Date"] = listLog[i].LogDate;
                            row["Amount"] = listLog[i].Amount;
                            row["Details"] = listLog[i].Details;
                            dt.Rows.Add(row);
                        }
                    }
                    gvView.DataSource = dt;
                    gvView.DataBind();
                }

                if (this.Request.Params["id"] == "5")
                {
                    for (int i = 0; i < listLog.Count; i++)
                    {
                        DateTime ngayGD = listLog[i].LogDate;
                        TimeSpan time = ngayHT - ngayGD;
                        if (time.Days <= 365)
                        {
                            DataRow row = dt.NewRow();
                            row["ATM(Location)"] = listLog[i].ATM.ATMAddress;
                            row["Type"] = listLog[i].LogType.Description;
                            row["Date"] = listLog[i].LogDate;
                            row["Amount"] = listLog[i].Amount;
                            row["Details"] = listLog[i].Details;
                            dt.Rows.Add(row);
                        }
                    }
                    gvView.DataSource = dt;
                    gvView.DataBind();
                }

                if (this.Request.Params["id"] == "6")
                {
                    for (int i = 0; i < listLog.Count; i++)
                    {
                        DateTime ngayGD = listLog[i].LogDate;
                        TimeSpan time = ngayHT - ngayGD;
                        if (time.Days <= 830)
                        {
                            DataRow row = dt.NewRow();
                            row["ATM(Location)"] = listLog[i].ATM.ATMAddress;
                            row["Type"] = listLog[i].LogType.Description;
                            row["Date"] = listLog[i].LogDate;
                            row["Amount"] = listLog[i].Amount;
                            row["Details"] = listLog[i].Details;
                            dt.Rows.Add(row);
                        }
                    }
                    gvView.DataSource = dt;
                    gvView.DataBind();
                }
            }

        protected void button3_Click(object sender, EventArgs e)
        {
            try
            {
                gvView.PageIndex -= 1;
                gvView.DataBind();
                Load();
            }
            catch
            {
            }
        }
        protected void button6_Click(object sender, EventArgs e)
        {
            try
            {
                gvView.PageIndex += 1;
                gvView.DataBind();
                Load();
            }
            catch { }
        }
        protected void buttonCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("http://localhost:2439/WithdrawPrintReceipt.aspx"); ;
        }
    
    }
}